package com.test.io.inputOutput;

import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *  输入流使用
 */
public class InputStreamDemo {

    // 使用输入流获取文件内容
    @Test
    public void readFile() {
        FileInputStream inputStream = null;
        try {
             inputStream = new FileInputStream("d:\\newFile2.text");

             byte[] readData = new byte[8];
             // 读取字节数
             int index = 0;
             while ((index = inputStream.read(readData)) != -1) {
                 System.out.print(new String(readData, 0, index));
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
